/**
 * @author: 李少斌
 * @create: 2019/1/3
 * @describe: 公共ajax配置
 */
"use strict";
import axios from "axios";
import * as Cookies from "tiny-cookie";
import router from "../router";
import qs from "qs"
import {Toast} from 'vue-ydui/dist/lib.rem/dialog';
// let loading = null;

var ajax = axios.create({
  baseURL: "/",
  headers: {/*"token": TOKEN || "",*/"Content-Type": "application/json;charset=UTF-8"},
  // headers:{"ttsx_auth_token": TOKEN,"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"},
  //`transformResponse`选项允许我们在数据传送到`then/catch`方法之前对数据进行改动
  transformResponse:[function(data){
    //在这里根据自己的需求改变数据
    // layer.close(loading);
    var temData = JSON.parse(data);
    if(temData.code == '200'){
      return temData.data
    }else if(temData.code == '500'){
      Toast({
          mes: temData.msg,
          timeout: 1500
      });
    }/*else{
      this.$dialog.toast({
          mes: '请输入手机号',
          timeout: 1500
      });
    }*/
    // return temData;
  }],
  transformRequest:[function(data){
      //在这里根据自己的需求改变数据
     // var obj= qs.stringify(data)
     return data
  }],
});

//添加一个请求拦截器
ajax.interceptors.request.use(function(config){
  //在请求发出之前进行一些操作
  let TOKEN = localStorage.getItem("ydtoken")||"";
  config.headers["Authorization"] = 'Bearer '+TOKEN;
  return config;
},function(err){
  // layer.close(loading);
  //Do something with request error
  return Promise.reject(err);
});

export default ajax;
